#include "cljmudx10baseapp.h"


//------------------PUBLIC METHODS----------------------------------------------------------------------------

/******************************************
* Initialise our Application Object
******************************************/
bool CDX10BaseApplication::initialise()
{
	//If we have not got an associated Windows App then we cannot
	//continue - when our app is added to the windows app, this should 
	//automatically occur.
	if(_obj_wndapp != NULL)
	{
		// Create the Direct3D object.
		_obj_dxman = new CDX10Manager(this->_obj_wndapp);
		if(!_obj_dxman)
			return false;
		
		bool tsuccess = _obj_dxman->initialise();
		if(!tsuccess)
			return false;

		// Create an left handed orthographic projection matrix for 2D rendering.
		D3DXMatrixOrthoOffCenterLH(&this->_dx_mat_ortho,0,(float)this->getScreenWidth(),0,(float)this->getScreenHeight(),this->getCamNearPlane(),this->getCamFarPlane());
		
		D3DXVECTOR3 tdxvec_up	= D3DXVECTOR3(0.0f,1.0f,0.0f);
		D3DXVECTOR3 tdxvec_pos	= D3DXVECTOR3(0.0f,0.0f,-1.0f);
		D3DXVECTOR3 tdxvec_fwd  = D3DXVECTOR3(0.0f,0.0f,1.0f);
		// Finally create the view matrix from the three updated vectors.
		D3DXMatrixLookAtLH(&this->_dx_mat_view, &tdxvec_pos, &tdxvec_fwd, &tdxvec_up);

		//Only Call the Custom Initialise if we have a valid Windows App and Valid DX10 Manager
		return this->customInit();
	}
	return false;
}

/*******************************************
* Update the Game.  We have no implicit 
* update handling so just call the custom
* Update in the concrete game implementation.
*******************************************/
bool CDX10BaseApplication::update()
{
	return this->customUpdate();
}

/******************************************
* Render our DirectX 10 Scene by using the
* DX10 Manager to perform the following steps:
* - Begin Rendering
* - Switch Off Z-Depth Testing
* - Call our Game's Concrete Render Method
* - Disable the Two-D Rendering
* - End the Rendering Process
******************************************/
bool CDX10BaseApplication::render()
{
	// Clear the buffers to begin the scene.
	this->_obj_dxman->beginRendering();

	// Turn off the Z buffer to begin all 2D rendering.
	this->_obj_dxman->enableTwoDimMode();

	//Call Our Abstract Render Method
	this->customRender();
	
	// Turn the Z buffer back on now that all 2D rendering has completed.
	this->_obj_dxman->disableTwoDimMode();

	// Present the rendered scene to the screen.
	this->_obj_dxman->endRendering();

	//We have Successfully rendered to return true
	return true;
}

/******************************************
* Cleanup Any Dynamically Assigned Memory
* Objects.
******************************************/
void CDX10BaseApplication::cleanup()
{
	//Use the Custom Cleanup of the class to delete any assigned memory in our
	//concrete game implementation.
	this->customCleanup();

	//If the DX Manager Exists - Clean It Up
	if(this->_obj_dxman)
	{
		//We want to cleanup the DirectX 10 Manager, as we are responsible for it.
		this->_obj_dxman->cleanup();
		delete this->_obj_dxman;
		this->_obj_dxman = NULL;
	}

	if(this->_obj_wndapp)
	{
		//We don't want to delete the Windows App, let the Windows App delete us instead
		this->_obj_wndapp = NULL;
	}
	return;
}